package com.vargas.studypartner;

import java.util.ArrayList;

import Domain.Deck;
import Persistence.DeckRepository;
import Util.DeckListAdapter;
import Util.ErrorHelper;
import View.NewDeckDialog;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

/**
 * Activity for managing study decks.
 * */
public class ManageDecksActivity extends Activity{
	
	private Context _Context;
	protected NewDeckDialog _NewDeckDialog;
	protected DeckRepository _DeckRepository;
	
	/**
	 * Creates objects and sets event listeners to be used by this activity
	 * @param	savedInstanceState	The instance state of the application
	 * */
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.decks_dialog);
		
		_Context = this.getApplicationContext();
		_DeckRepository = new DeckRepository(_Context);
		
		Button btnNew = (Button)findViewById(R.id.btnNewDeck);
		btnNew.setOnClickListener(CreateNewButtonClickListener());
		
		Button btnOpen = (Button)findViewById(R.id.btnOpenDeck);
		btnOpen.setOnClickListener(CreateOpenButtonClickListener());
		btnOpen.setEnabled(false);
		
		ListView deckList = (ListView)findViewById(R.id.lstSavedDecks);
		deckList.setOnItemClickListener(CreateDeckListItemClickListener());
	}
	
	/**
	 * Calls super onResume and loads decks from the database into the 
	 * list view.
	 * */
	@Override
	public void onResume(){
		super.onResume();
		LoadDecks();
	}
	
	/**
	 * Creates the New button click listener that initializes and show the 
	 * new deck dialog.
	 * @return	{@link OnClickListener}
	 * */
	private OnClickListener CreateNewButtonClickListener() {
		return new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					if (_NewDeckDialog == null) {
						_NewDeckDialog = new NewDeckDialog(ManageDecksActivity.this);
					}
					_NewDeckDialog.show();
				} catch(Exception e){
					ErrorHelper.reportError(ManageDecksActivity.this, e);
				}
			}
		};
	}
	
	/**
	 * Creates the Open button click listener that opens the selected deck 
	 * from the list view.
	 * @return	{@link OnClickListener}
	 * */
	private OnClickListener CreateOpenButtonClickListener() {
		return new OnClickListener() {
			@Override
			public void onClick(View v) {
				ListView listView = (ListView) v.getRootView().findViewById(R.id.lstSavedDecks);
				int selected = listView.getSelectedItemPosition();
				if (selected >= 0) {
					//TODO: open the deck activity
				} else {
					v.setEnabled(false);
				}
			}
		};
	}
	
	/**
	 * Creates the list view item click listener that enables the Open button
	 * when an Item is selected.
	 * @return	{@link OnItemClickListener}
	 * */
	private OnItemClickListener CreateDeckListItemClickListener() {
		return new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Button btnOpen = (Button)view.getRootView().findViewById(R.id.btnOpenDeck);
				btnOpen.setEnabled(true);
			}
		};
	}
	
	/**
	 * Loads decks from the database into the list view.
	 * */
	private void LoadDecks() {
		try {
			ArrayList<Deck> deckList = (ArrayList<Deck>) _DeckRepository.getAllRows();
			ListView listView = (ListView)findViewById(R.id.lstSavedDecks);
			listView.setAdapter(new DeckListAdapter(this, R.layout.decks_dialog_listitem, deckList));
		} catch(Exception e) {
			ErrorHelper.reportError(ManageDecksActivity.this, e);
		}
	}
}
